﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;

namespace Common.Utils
{
    public static class PortHelp
    {
        public static List<string> GetPorts()
        {
            //获取本地配置文件（注册表文件）
            RegistryKey registryKey = Registry.LocalMachine;

            //读取HARDWARE节点的键值
            RegistryKey software = registryKey.OpenSubKey("HARDWARE");

            //打开HARDWARE子健

            RegistryKey devicemap = software.OpenSubKey("DEVICEMAP");
            RegistryKey siteKey = devicemap.OpenSubKey("SERIALCOMM");

            //获取当前子健
            List<string> portList = new List<string>();
            if (siteKey != null)
            {
                string[] siteKeys = siteKey.GetValueNames();

                for (int i = 0; i < siteKeys.Count(); i++)
                {
                    if (siteKeys[i].ToLower().Contains("serial"))
                    {
                        object obj = siteKey.GetValue(siteKeys[i]);
                        portList.Add(obj.ToString());
                    }
                }
            }

            return portList;
        }
    }
}
